

package kernel


type Kernel func (i,j int) (bool)


// TODO: block is ignored for now
// kernel: a closure which will be invoked for each i,j in gridSize
// return chan: true/false on success/failure when all kernels have finished
func Launch (rows,cols int, kernel Kernel) (<-chan bool) {

	res := make (chan bool)
	done := make (chan bool)
	success := true

	// wait for kernels to finish
	go func () {
		for i := 0; i < rows; i++ {
			for j := 0; j < cols; j++ {
				success = success && <-done
			}
		}	
		res <- success		
	} ()

	// call each kernel in a separate goroutine
	for i := 0; i < rows; i++ {
		for j := 0; j < cols; j++ {
			go func (i,j int) {
				done <- kernel (i,j)
			} (i,j)
		}
	}
	
	return res
}

